package inventoryservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
)

type AssetDisposalPageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetDisposalPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetDisposalPageLogic {
	return &AssetDisposalPageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AssetDisposalPage 分页查询资产报废表项
func (l *AssetDisposalPageLogic) AssetDisposalPage(in *property_service.AssetDisposalPageReq) (*property_service.AssetDisposalPageRes, error) {
	// 定义一个资产报废表模型对象切片用于存储查询结果
	var data []*model.AssetDisposal
	// 获取资产报废表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetDisposal
	// 构建查询条件：根据租户ID查询
	do := m.WithContext(l.ctx).Where(m.TenantID.Eq(in.Where.TenantID))
	// 动态构建查询条件
	if in.Where.ID > 0 {
		do = do.Where(m.ID.Eq(in.Where.ID)) // ID
	} else {
		// 处理字符类型字段，进行模糊查询
		if in.Where.DisposalCode != "" {
			do = do.Where(m.DisposalCode.Like("%" + in.Where.DisposalCode + "%")) // 报废单号
		}
		if in.Where.AssetCode != "" {
			do = do.Where(m.AssetCode.Like("%" + in.Where.AssetCode + "%")) // 资产编号
		}
		if in.Where.AssetName != "" {
			do = do.Where(m.AssetName.Like("%" + in.Where.AssetName + "%")) // 资产名称
		}
		if in.Where.SpecModel != "" {
			do = do.Where(m.SpecModel.Like("%" + in.Where.SpecModel + "%")) // 规格型号
		}
		if in.Where.AssetCategory != "" {
			do = do.Where(m.AssetCategory.Like("%" + in.Where.AssetCategory + "%")) // 资产分类
		}
		if in.Where.FinancialCategory != "" {
			do = do.Where(m.FinancialCategory.Like("%" + in.Where.FinancialCategory + "%")) // 财务分类
		}
		if in.Where.UsingDept != "" {
			do = do.Where(m.UsingDept.Like("%" + in.Where.UsingDept + "%")) // 使用科室
		}
		if in.Where.DeptResponsible != "" {
			do = do.Where(m.DeptResponsible.Like("%" + in.Where.DeptResponsible + "%")) // 科室责任人
		}
		if in.Where.Unit != "" {
			do = do.Where(m.Unit.Like("%" + in.Where.Unit + "%")) // 单位
		}
		if in.Where.StateAssetCode != "" {
			do = do.Where(m.StateAssetCode.Like("%" + in.Where.StateAssetCode + "%")) // 国资编码
		}
		if in.Where.IsRetained != "" {
			do = do.Where(m.IsRetained.Like("%" + in.Where.IsRetained + "%")) // 科室是否留用
		}
		if in.Where.DisposalReason != "" {
			do = do.Where(m.DisposalReason.Like("%" + in.Where.DisposalReason + "%")) // 报废原因备注
		}
		if in.Where.Attachment != "" {
			do = do.Where(m.Attachment.Like("%" + in.Where.Attachment + "%")) // 附件
		}
		if in.Where.StatusName != "" {
			do = do.Where(m.StatusName.Like("%" + in.Where.StatusName + "%")) // 采购状态
		}
		if in.Where.DisposalStatus != "" {
			do = do.Where(m.DisposalStatus.Like("%" + in.Where.DisposalStatus + "%")) // 状态
		}

		// 处理时间类型字段，进行精确查询
		if in.Where.AcceptanceDate != "" {
			do = do.Where(m.AcceptanceDate.Eq(BeanUtils.GetDateFormat(in.Where.AcceptanceDate))) // 验收日期
		}

		// 处理数字类型字段，进行精确查询
		if in.Where.AssetID > 0 {
			do = do.Where(m.AssetID.Eq(in.Where.AssetID)) // 资产ID
		}
		if in.Where.AssetCategoryID > 0 {
			do = do.Where(m.AssetCategoryID.Eq(in.Where.AssetCategoryID)) // 资产分类ID
		}
		if in.Where.FinancialCategoryID > 0 {
			do = do.Where(m.FinancialCategoryID.Eq(in.Where.FinancialCategoryID)) // 财务分类ID
		}
		if in.Where.UsingDeptID > 0 {
			do = do.Where(m.UsingDeptID.Eq(in.Where.UsingDeptID)) // 使用科室ID
		}
		if in.Where.DeptResponsibleID > 0 {
			do = do.Where(m.DeptResponsibleID.Eq(in.Where.DeptResponsibleID)) // 科室责任人ID
		}
		if in.Where.StatusID > 0 {
			do = do.Where(m.StatusID.Eq(in.Where.StatusID)) // 状态ID
		}
		if in.Where.StockID > 0 {
			do = do.Where(m.StockID.Eq(in.Where.StockID)) // 仓库ID
		}
	}

	// 定义一个变量用于存储总记录数
	var count int64

	// 判断是否需要分页
	if in.Page+in.Limit <= 1 {
		// 如果不需要分页，查询所有记录
		data, _ = do.Find()
		// 设置总记录数为查询结果的长度
		count = int64(len(data))
	} else {
		// 如果需要分页，查询指定页的数据和总记录数
		data, count, _ = do.FindByPage(int((in.Page-1)*in.Limit), int(in.Limit))
	}

	// 将查询结果转换为响应对象
	list, _ := BeanUtils.CopyProperties(data, property_service.AssetDisposal{})

	// 返回查询结果，包括总记录数和数据列表
	return &property_service.AssetDisposalPageRes{
		Count: count,
		List:  list.([]*property_service.AssetDisposal),
	}, nil
}
